package com.purchase.ml.service.log.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.purchase.ml.bean.req.log.OperationLogQueryDto;
import com.purchase.ml.entity.log.OperationLog;
import com.purchase.ml.mapper.sys.SysUserMapper;
import com.purchase.ml.mapper.log.OperationLogMapper;
import com.purchase.ml.service.log.OperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kyyang
 * Created 2021-01-07
 */
@Service
@Slf4j
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {

    @Autowired
    private OperationLogMapper operationLogMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public IPage<OperationLog> operationLogQuery(OperationLogQueryDto operationLogQueryDto) {
        IPage<OperationLog> page = new Page<>(operationLogQueryDto.getPageIndex(),operationLogQueryDto.getPageNum());
        QueryWrapper<OperationLog> wrapper = new QueryWrapper<>();

        if(StringUtils.isNotBlank(operationLogQueryDto.getOperType())){
            wrapper.eq("oper_type",operationLogQueryDto.getOperType());
        }
        if(StringUtils.isNotBlank(operationLogQueryDto.getOperUserName())){
            wrapper.eq("oper_user_id",operationLogQueryDto.getOperUserName());
        }
        if(operationLogQueryDto.getStartTime()!=null){
            wrapper.ge("oper_create_time",operationLogQueryDto.getStartTime());
        }
        if(operationLogQueryDto.getEndTime()!=null){
             wrapper.le("oper_create_time",operationLogQueryDto.getEndTime());
        }

        wrapper.orderByDesc("oper_create_time");
        IPage<OperationLog> operationLogIPage = operationLogMapper.selectPage(page, wrapper.orderByDesc("id"));
        return operationLogIPage;
    }
}
